<template>
  <div class="case-converter">
    <el-card class="tool-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <h3>大小写转换工具</h3>
          <p class="card-description">快速转换文本的大小写格式，支持多种转换模式</p>
        </div>
      </template>
      
      <div class="tool-content">
        <div class="input-section">
          <h4 class="section-title">输入文本</h4>
          <el-form label-position="top">
            <el-form-item>
              <el-input
                v-model="inputText"
                type="textarea"
                :rows="8"
                placeholder="请输入要转换的文本..."
              />
            </el-form-item>
          </el-form>
        </div>
        
        <div class="options-section">
          <h4 class="section-title">转换模式</h4>
          <div class="options-wrapper">
            <el-radio-group v-model="convertType" @change="updatePreview">
              <el-radio-button label="uppercase">全部大写</el-radio-button>
              <el-radio-button label="lowercase">全部小写</el-radio-button>
              <el-radio-button label="titlecase">首字母大写</el-radio-button>
              <el-radio-button label="sentencecase">句首大写</el-radio-button>
            </el-radio-group>
          </div>
        </div>
        
        <div class="result-section">
          <h4 class="section-title">转换结果</h4>
          <div class="preview-container">
            <div v-if="convertedText" class="preview-content">{{ convertedText }}</div>
            <div v-else class="preview-placeholder">转换后的内容将显示在这里...</div>
          </div>
        </div>
        
        <div class="actions-section">
          <el-button type="primary" @click="copyResult" :disabled="!convertedText">复制结果</el-button>
          <el-button @click="clearText" :disabled="!inputText">清空文本</el-button>
        </div>
      </div>
      
      <div class="guide-section">
        <h4 class="section-title">快速使用指南</h4>
        <div class="guide-container">
          <div v-for="(step, index) in usageSteps" :key="index" class="guide-step">
            <div class="step-number">{{ index + 1 }}</div>
            <div class="step-content">
              <div class="step-text">{{ step }}</div>
            </div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
/**
 * 大小写转换组件
 * 提供文本大小写转换功能
 */
import { ref, watch } from 'vue';
import { ElMessage } from 'element-plus';

// 输入文本
const inputText = ref('');
// 转换类型
const convertType = ref('uppercase');
// 转换后的文本
const convertedText = ref('');

// 使用步骤
const usageSteps = [
  '在输入框中粘贴或输入需要转换的文本',
  '选择转换类型（全部大写、全部小写、首字母大写或句首大写）',
  '查看预览结果',
  '使用"复制结果"按钮复制转换后的文本'
];

// 更新预览
const updatePreview = () => {
  if (!inputText.value) {
    convertedText.value = '';
    return;
  }

  switch (convertType.value) {
    case 'uppercase':
      convertedText.value = inputText.value.toUpperCase();
      break;
    case 'lowercase':
      convertedText.value = inputText.value.toLowerCase();
      break;
    case 'titlecase':
      convertedText.value = inputText.value
        .split(' ')
        .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
        .join(' ');
      break;
    case 'sentencecase':
      convertedText.value = inputText.value
        .toLowerCase()
        .replace(/(^\s*\w|[.!?]\s*\w)/g, function(c) {
          return c.toUpperCase();
        });
      break;
  }
};

// 复制结果
const copyResult = () => {
  if (!convertedText.value) {
    ElMessage.warning('没有可复制的内容');
    return;
  }

  navigator.clipboard.writeText(convertedText.value)
    .then(() => {
      ElMessage.success('转换结果已复制');
    })
    .catch(() => {
      ElMessage.error('复制失败');
    });
};

// 清空文本
const clearText = () => {
  inputText.value = '';
  convertedText.value = '';
};

// 监听输入变化
watch(inputText, () => {
  updatePreview();
});

// 初始化
updatePreview();
</script>

<style scoped>
.case-converter {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.tool-card {
  border-radius: 8px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.card-header {
  padding-bottom: 15px;
}

.card-header h3 {
  font-size: 18px;
  color: #303133;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.card-description {
  font-size: 14px;
  color: #606266;
  margin: 0;
}

.tool-content {
  padding: 5px 0;
}

.section-title {
  font-size: 16px;
  color: #303133;
  margin-bottom: 15px;
  font-weight: 500;
  position: relative;
  padding-left: 12px;
}

.section-title:before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 16px;
  background-color: #3498db;
  border-radius: 2px;
}

.input-section {
  margin-bottom: 25px;
}

.options-section {
  margin-bottom: 25px;
}

.options-wrapper {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  gap: 10px;
}

.result-section {
  margin-bottom: 25px;
}

.preview-container {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  min-height: 120px;
  background-color: #f8f9fa;
  padding: 15px;
}

.preview-content {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
}

.preview-placeholder {
  color: #909399;
  text-align: center;
  padding: 30px 0;
  font-style: italic;
}

.actions-section {
  margin: 25px 0;
  display: flex;
  gap: 10px;
}

.guide-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px dashed #e4e7ed;
}

.guide-container {
  display: flex;
  justify-content: space-between;
  gap: 15px;
  margin-top: 20px;
}

.guide-step {
  flex: 1;
  background-color: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

.guide-step:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.step-number {
  width: 32px;
  height: 32px;
  background-color: #3498db;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-bottom: 12px;
}

.step-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.step-text {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

@media (max-width: 768px) {
  .options-wrapper {
    flex-direction: column;
    align-items: flex-start;
  }
  
  :deep(.el-radio-group) {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }
  
  :deep(.el-radio-button) {
    margin-right: 0;
  }
  
  :deep(.el-radio-button__inner) {
    border-radius: 4px !important;
    border: 1px solid #dcdfe6 !important;
  }
  
  .actions-section {
    flex-wrap: wrap;
  }
  
  .guide-container {
    flex-direction: column;
  }
  
  .guide-step {
    flex-direction: row;
    text-align: left;
    padding: 15px;
    margin-bottom: 8px;
  }
  
  .step-number {
    margin-bottom: 0;
    margin-right: 15px;
  }
}
</style> 